package com.example.demo.Controller;

import com.example.demo.pojo.Cart;
import com.example.demo.pojo.CartItem;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/myCart1")
public class CartController {
    @Resource
    Cart cart;



    @GetMapping("/showCart1")
    public  String showCart(HttpSession session){
        Cart cart =(Cart) session.getAttribute("cart");

        session.setAttribute("CartTotalPrice",cart.GetTotalPrice());

        return  "cart";
    }
    @RequestMapping("/addCart")
    @ResponseBody // 这将告诉Spring将返回值转换为JSON
    public Map<String, Object> addCart(@RequestParam("id") Integer id,
                                       @RequestParam("name") String name,
                                       @RequestParam("price") double price,
                                       HttpSession session, Model model) {

        // 检查会话中是否已经有购物车，如果没有则创建一个
        Cart cart = (Cart) session.getAttribute("cart");
        if (cart == null) {
            cart = new Cart();
            session.setAttribute("cart", cart);
        }

        CartItem cartItem = new CartItem();
        cartItem.setId(id);
        cartItem.setName(name);
        cartItem.setPrice(new BigDecimal(price));
        cartItem.setTotalPrice(new BigDecimal(price).multiply(new BigDecimal(cartItem.getCount())));
        cart.addItem(cartItem);
        session.setAttribute("FID",id);
        model.addAttribute("pid",id);

        // 示例：返回商品ID和可能的其他信息
        Map<String, Object> response = new HashMap<>();
        response.put("itemId", id);
        response.put("message", "Item added to cart successfully.");
        // 也可以添加购物车中商品的总数
        response.put("totalItems", cart.getTotalCount());

        return response;
    }

    @GetMapping("/deleteCart")
    public  String  delete(HttpSession session){
        Cart cart1 = new Cart();
        session.setAttribute("cart",cart1);
        session.removeAttribute("CartTotalPrice");
        return  "cart";
    }

    @GetMapping("updateCart")
    public  String  updateCart(@RequestParam(defaultValue = "1") Integer count , Integer id, HttpSession session, HttpServletResponse response,HttpServletRequest request) throws IOException {
        Cart cart =(Cart) session.getAttribute("cart");
        if (cart!=null){
            cart.updateCount(id,count);
            session.setAttribute("CartTotalPrice",cart.GetTotalPrice());
        }


        return  "cart";

    }
    @GetMapping("/deleteCart1")
    public String deleteCart(@RequestParam Integer id,HttpSession session){

        Cart cart =(Cart) session.getAttribute("cart");
        cart.deleteItem(id);
        session.setAttribute("CartTotalPrice",cart.GetTotalPrice());
        return "cart";
    }

}

